# -*- coding: utf-8 -*-
# @Time    : 2019/10/16 17:12
# @Author  : LiHaowen
# @Email   : lihaowen2017@163.com
# @File    : list_gen.py
# 列表生成式(列表生成式)
# 1. 提取出1-20之间的奇数
# res = [x for x in range(1, 21) if x % 2]

# 2. 逻辑复杂的情况 求1-20中奇数平方


def hanle_item(item):
    return item * item


res = [x ** 2 for x in range(1, 21) if x % 2]


# 列表生成式性能高于列表操作

# 生成器表达式

res_gen = (x ** 2 for x in range(1, 21) if x % 2)
print(type(res_gen))
# for i in res_gen:  # 生成器获取数据
#     print(i)
res_list = list(res_gen)  # list 接受可迭代对象
print(res_list)

# 字典推导式
my_dict = {"a1": 23, "b1": 24, "c1": 5, "d1": 23}
print(my_dict.items())
# key value 颠倒
reversed_dict = {value:key for key, value in my_dict.items()}
print(reversed_dict)

# 集合推导式
my_set = {value for key, value in my_dict.items()}
print(type(my_set))
print(my_set)
# my_set_key = set(my_dict.keys())  # dict 接口调用
# print(my_set_key)
